package cn.myfirefly.moming.common.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.myfirefly.moming.common.dao.mapper.RoleDataAuthMapper;
import cn.myfirefly.moming.common.entity.MmRoleDataAuth;
import cn.myfirefly.moming.common.model.DepartmentDto;
import cn.myfirefly.moming.common.model.RoleDataAuthDto;
import cn.myfirefly.moming.common.model.UserDto;
import cn.myfirefly.core.BaseDao;
import cn.myfirefly.utils.CollectionUtil;

@Repository
public class RoleDataAuthDao extends BaseDao<MmRoleDataAuth> {

	@Autowired
	private RoleDataAuthMapper roleDataAuthMapper;
	
	public RoleDataAuthDto getRoleDataAuth(String funId, Integer roleSn) {
		List<RoleDataAuthDto> list = roleDataAuthMapper.getRoleDataAuth(funId, roleSn);
		if (CollectionUtil.isNotEmpty(list)) {
			return list.get(0);
		}
		return null;
	}
	
	public List<RoleDataAuthDto> getRoleDataAuthList(Integer systemId, String loginName) {
		List<RoleDataAuthDto> list = roleDataAuthMapper.getRoleDataAuthList(systemId, loginName);
		return list;
	}
	
	public Integer getDeptIdByLoginName(String loginName) {
		List<DepartmentDto> list = roleDataAuthMapper.getDeptByLoginName(loginName);
		if (CollectionUtil.isNotEmpty(list)) {
			return list.get(0).getDepartmentSn();
		}
		return null;
	}
	
	public String getDeptNameByLoginName(String loginName) {
		List<DepartmentDto> list = roleDataAuthMapper.getDeptByLoginName(loginName);
		if (CollectionUtil.isNotEmpty(list)) {
			return list.get(0).getDepartmentName();
		}
		return null;
	}
	
	public List<Integer> getUserIdByDeptId(Integer deptId) {
		List<Integer> resultList = null;
		List<UserDto> list = roleDataAuthMapper.getUserByDeptId(deptId);
		if (CollectionUtil.isNotEmpty(list)) {
			resultList = new ArrayList<>();
			for (UserDto userDto : list) {
				resultList.add(userDto.getUserSn());
			}
		}
		return resultList;
	}
	
	public List<String> getUserNameByDeptId(Integer deptId) {
		List<String> resultList = null;
		List<UserDto> list = roleDataAuthMapper.getUserByDeptId(deptId);
		if (CollectionUtil.isNotEmpty(list)) {
			resultList = new ArrayList<>();
			for (UserDto userDto : list) {
				resultList.add(userDto.getUserName());
			}
		}
		return resultList;
	}
}
